package com.example.systemend;

import io.jsonwebtoken.*;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;
import java.util.UUID;

/**
 * @author 权哥
 * @version 1.0
 * @date 2022/6/10 9:13
 */
@SpringBootTest
public class test {

    private long time=1000*60*60*24;
    private String signature="LiQuan";
    @Test
    public void jwt(){
        JwtBuilder jwtBuilder= Jwts.builder();
        String jwtToken=jwtBuilder
                //header头
                .setHeaderParam("typ","TWT")
                .setHeaderParam("alg","HS256")
                //payload载荷
                .claim("username","李权")
                .claim("role","admin")
                .setSubject("test_li")
                .setExpiration(new Date(System.currentTimeMillis()+time))
                .setId(UUID.randomUUID().toString())
                //signature  签名
                .signWith(SignatureAlgorithm.HS256,signature)
                .compact();

        System.out.println(jwtToken);
    }

    @Test
    public void parse(){
        String token="eyJ0eXAiOiJUV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6IuadjuadgyIsInBhc3N3b3JkIjoiTFEyMDAxMDMxMSIsInN1YiI6InRlc3RfbGkiLCJleHAiOjE2NTQ5MzExMzksImp0aSI6IjJhMjU3MTYwLThmN2YtNGE4YS1iYWZjLWVlYmQ0OTNlZjdkZCJ9.GjWfFVdw21ntpx-4Byz_ruGjnUSCHqS7SFS5k_RVZTk";
        JwtParser jwtParser=Jwts.parser();
        Jws<Claims> claimsJws=jwtParser.setSigningKey(signature).parseClaimsJws(token);
        Claims claims=claimsJws.getBody();
        System.out.println(claims.get("username"));
        System.out.println(claims.get("password"));
        System.out.println(claims.getId());
        System.out.println(claims.getSubject());
        System.out.println(claims.getExpiration());
    }

}
